//# sourceURL=roleDetail.js
layui.element.on('tab(login-user-tabs)', function (layui_obj) {
    var zTree = $.fn.zTree.getZTreeObj('roleDetail-permission-tree');
    var custom_obj = $('#tabTitles')[0].children[layui_obj.index].tabCustomObj;
    var roleDetail_fn = {
        initRender: function () {
            cmsUtils.setContentTabHeight('roleDetailContent', 50);
            $.get('/cms/sysManage/getSysAllRights', function (data) {
                var tree_setting = {
                    data:{
                        simpleData:{
                            enable: true,
                            idKey: 'id',
                            pIdKey: 'parentId',
                            rootPId: 0
                        }
                    },
                    check:{
                        enable: true
                    }
                }
                var tree_data = {
                    id: 0,
                    name: '所有权限',
                    open: true,
                    children: data.data
                }
                zTree = $.fn.zTree.init($('#roleDetail-permission-tree'), tree_setting, tree_data);
                $('#roleDetail').attr('hasRender', true);
                if (custom_obj.id > 0) {
                    var url = '/cms/sysManage/getRoleById/'+custom_obj.id;
                    $.get(url, function (data) {
                        roleDetail_fn.setFormData(data);
                        $('button[lay-filter="roleDetail-btn-modify"]').show();
                        $('#roleDetail-div-dateCreated').show();
                    });
                } else {
                    $('button[lay-filter="roleDetail-btn-add"]').show();
                }
            });
        },
        setFormData: function (data) {
            cmsUtils.formDataBind('roleDetail-form', data.data.role_info);
            if (data.data.role_permissions.length > 0) {
                var tree_node = zTree.getNodeByParam('id', 0);
                zTree.checkNode(tree_node, true, false);
            }
            $.each(data.data.role_permissions, function (index, item) {
                var tree_node = zTree.getNodeByParam('id', item.id);
                tree_node.checkedOld = true;
                zTree.checkNode(tree_node, true, false);
            });
        },
        getFormData: function (data) {
            var rights_add = '', rights_del = '';
            var checkedNodes = zTree.getChangeCheckedNodes();
            $.each(checkedNodes, function (index, item) {
                if (item.id == 0) {
                    return;
                }
                if (!item.checkedOld) {
                    rights_add += rights_add ? (','+item.id) : item.id;
                } else {
                    rights_del += rights_del ? (','+item.id) : item.id;
                }
            });
            data.id = (custom_obj.id > 0) ? custom_obj.id : 0;
            data['rights_add'] = rights_add;
            data['rights_del'] = rights_del;
            return data;
        },
        submitForm: function (post_params, action, index) {
            var post_url = (custom_obj.id > 0) ? '/cms/sysManage/editRole' : '/cms/sysManage/addRole';
            $.ajax({
                type: 'post',
                url: post_url,
                data: post_params,
                dataType: 'json',
                success: function (data) {
                    layer.close(index);
                    if (data.code == 200) {
                        switch (action) {
                            case 'continue':
                                $('#roleDetail-form')[0].reset();
                                zTree.checkAllNodes(false);
                                break;
                            case 'close':
                                layui.element.tabDelete('login-user-tabs', 'tab-roleDetail');
                                custom_obj.list_fn.reload();
                                break;
                        }
                    } else {
                        layer.msg(data.msg, {anim: 6});
                    }
                }
            });
        }
    }
    if ($('#roleDetail')[0] && !$('#roleDetail').attr('hasRender')) {
        roleDetail_fn.initRender();
        // 设置页面高度
        $(window).resize(function() {
            cmsUtils.setContentTabHeight('roleDetailContent', 50);
        });
    }
    // 添加
    layui.form.on('submit(roleDetail-btn-add)', function (data) {
        layer.confirm('确定添加该角色吗？', {
            btn:['确定', '取消']
        }, function (index) {
            roleDetail_fn.submitForm(roleDetail_fn.getFormData(data.field), 'close', index);
        });
    });
    // 修改
    layui.form.on('submit(roleDetail-btn-modify)', function (data) {
        layer.confirm('确定完成该角色修改吗？', {
            btn:['确定', '取消']
        }, function (index) {
            roleDetail_fn.submitForm(roleDetail_fn.getFormData(data.field), 'close', index);
        });
    });
});